package com.yehongyu.mytools.basic.designpattern.singleton;

import junit.framework.Assert;
import junit.framework.TestCase;

public class ClassicSingletonTest extends TestCase {
//	private ClassicSingleton sone = null, stwo = null;
//	private static Logger logger = Logger.getRootLogger();
	public ClassicSingletonTest(String name) {
		super(name);
	}

/*	protected void setUp() throws Exception {
		super.setUp();
//		logger.info("获取单例...");
		System.out.println("获取单例...");
		sone = ClassicSingleton.getInstance();
//		logger.info("...获取单例为:"+sone);
		System.out.println("...获取单例为:"+sone);
//		logger.info("获取单例...");
		System.out.println("获取单例...");
		stwo = ClassicSingleton.getInstance();
//		logger.info("...获取单例为:"+stwo);
		System.out.println("...获取单例为:"+stwo);
	}*/

	/*
	 * Test method for 'com.yehy.pattern.singleton.ClassicSingleton.getInstance()'
	 */
	/*public void testGetInstance() {
//		logger.info("检查单例是否相等");
		Assert.assertEquals(true,sone==stwo);
	}*/
	
	private static ClassicSingleton singleton = null;
	
	public void setUp(){
		singleton = null;
	}
	
	public void testGetInstanceWhenMulThread() throws InterruptedException{
		Thread threadOne = new Thread(new ClassicSingletonTestRunnable()),
				threadTwo = new Thread(new ClassicSingletonTestRunnable());
		threadOne.start();
		threadTwo.start();
		threadOne.join();
		threadTwo.join();
	}


	private static class ClassicSingletonTestRunnable implements Runnable{
		public void run(){
			ClassicSingleton s = ClassicSingleton.getInstanceWhenMulThread();
			
			synchronized(ClassicSingletonTest.class){
				if(singleton==null){
					singleton = s;
				}
			}
			Assert.assertEquals(true,s==singleton);
		}
	}

}
